﻿
/** Digitalfx.tv package */
package tv.digitalfx
{
	
import pete.layout.*;
import pete.objects.*;
import pete.tools.*;

import flash.display.*;
import flash.events.*;
import flash.text.*;






/**
 * @version
 * 0.1
 *
 * @description 
 * Create the graphics and data for the Launcher's options/descriptions by looping through a given XML Object.
 * 
 * @example
 * -----
 */
public class Option extends EventDispatcher
{
	
	
	
	
	
	
	// Properties
	// -----------------------------------------------------------
	
	/**
	 * Sets and returns the current Option.
	 */
	public function get currentOption():*
	{
		return _currentOption;
	}
	
	public function set currentOption( option:* ):void
	{
		_currentOption = option;
	}
	
	private var _currentOption:* = this;
	
	
	/**
	 * Returns the current list of the current Option.
	 */
	public function get currentList():*
	{
		return currentOption.list;
	}
	
	
	/**
	 * Title of the Option.
	 */
	public var title:String;
	
	
	/**
	 * Short description of the Option.
	 */
	public var description:String;
	
	
	/**
	 * A Cycle of all options contained within this Option.
	 */
	public var list:Cycle = new Cycle();
	
	
	/**
	 * The parent option to this Option.
	 */
	public var parent:*;
	
	
	/**
	 * The graphic of this Option.
	 */
	public var optGraphic:Sprite = new Sprite();
	
	
	/**
	 * The graphic of this Option.
	 */
	public var listHolder:Sprite = new Sprite();
	
	
	/**
	 * The graphic of the description of this Option.
	 */
	public var descGraphic:Sprite = new Sprite();
	
	
	
	
	
	
	// Constructor
	// -----------------------------------------------------------
	
	
	/**
	 * Creates a new instance of Option, which is used to create the graphics and data for the Launcher's options/descriptions.
	 * @param xml XML to parse through and create the data for the Option.
	 */
	public function Option( xml:XML )
	{
		this.title = xml.title;
		this.description = xml.description;
		
		drawGraphic();
		drawDescGraphic();
		
		optGraphic.addEventListener( MouseEvent.MOUSE_MOVE, handleMouseOver );
		descGraphic.addEventListener( MouseEvent.CLICK, handleClick );
		descGraphic.addEventListener( FocusEvent.FOCUS_IN, handleFocus );
		
		
		for each (var prop:XML in xml.section)
		{
			var option:Option = new Option( prop );
			option.parent = this;
			
			listHolder.addChild( option.optGraphic );
			list.push( option );
		}
	}
	
	
	
	
	
	
	// Control Functions
	// -----------------------------------------------------------
	
	/**
	 * Reverses the current selection of the current list. Loops to ending.
	 * 
	 * @see next()
	 */
	public function previous():void
	{
		currentList.previous();
	}
	
	
	/**
	 * Advances the current selection of the current list. Loops to begining.
	 * 
	 * @see previous()
	 */
	public function next():void
	{
		currentList.next();
	}
	
	
	/**
	 * Returns to the parent of the current Option.
	 * @return Boolean Boolean representing selection success.
	 * <code>true</code> means advance to the previous list.
	 * <code>false</code> means you are at the top.
	 */
	public function back():Boolean
	{
		if ( currentOption.parent )
		{
			currentOption = currentOption.parent;
			return true;
		}
		
		return false;
	}
	
	
	/**
	 * Returns to the first parent (inital Option).
	 */
	public function reset():void
	{
		while ( currentOption.parent )
		{
			currentOption = currentOption.parent;
		}
	}
	
	
	/**
	 * Selects the current Option.
	 * @return Boolean Boolean representing selection success.
	 * <code>true</code> means something was selected.
	 * <code>false</code> means advance to the next list.
	 */
	public function select():Boolean
	{
		if ( currentList.isDefault )
		{
			return true;
		}
		else if ( currentList.current.list[0] )
		{
			currentOption = currentList.current;
			return false;
		}
		
		return true;
	}
	
	
	
	
	
	
	// Drawing Functions
	// -----------------------------------------------------------
	
	private function drawGraphic():void
	{
		var box:Shape			= new Shape();
		box.graphics.lineStyle	( 1 );
		box.graphics.beginFill	( 0xffffff, 0 );
		box.graphics.drawRect	( 0, 0, 100, 30 );
		
		var text:TextField		= new TextField();
		text.selectable		= false;
		text.autoSize			= TextFieldAutoSize.LEFT;
		text.htmlText			= "<b>" + title + "</b>";
		text.x				= 10;
		text.mouseEnabled		= false;
		
		optGraphic.addChild		( box );
		optGraphic.addChild		( text );
		
		Align.centerVerticalTo	( text, box );
	}
	
	
	private function drawDescGraphic ():void
	{
		var text:TextField		= new TextField();
		text.selectable		= false;
		text.wordWrap			= true;
		text.width			= 180;
		text.autoSize			= TextFieldAutoSize.LEFT;
		text.htmlText			= "<b>" + title + "</b>\n" + description;
		text.x = text.y		= 10;
		
		descGraphic.addChild		( text );
	}
	
	
	
	
	
	
	// Event Handlers
	// -----------------------------------------------------------
	
	private function handleMouseOver( e:MouseEvent ):void
	{
		var l:Launcher = Launcher.getInstance();
		
		if ( l.active )
		{
			var cl:Cycle = this.parent.list;
			var o:int = cl.currentIndex;
			var c:int = cl.indexOf( this );
			
			if ( c > o )
			{
				l.nextOption( true );
			}
			else if ( c < o )
			{
				l.prevOption( true );
			}
		}
	}
	
	
	private function handleClick( e:MouseEvent ):void
	{
		var l:Launcher = Launcher.getInstance();
		l.nextList();
	}
	
	
	private function handleFocus( e:FocusEvent ):void
	{
		var s:Stage = e.target.stage;
		s.focus = s;
	}
	
}

}